#include "tableUtils.h"


void TableUtils::insertValue2Table(QTableWidget* table, int row, int col, QVariant val, int decimal, int alignment) {
    //判断表格的行数是否够
    int rows = table->rowCount();
    if (table->rowCount() < row + 1) {
        table->setRowCount(row + 1);
    }
    //转换数据
    QString text;
    if (val.type() == QMetaType::Float || val.type() == QMetaType::Double) {
        text = QString::number(val.toDouble(), 'f', decimal);
    } else {
        text = val.toString();
    }
    //填充数据
    QTableWidgetItem* item = new QTableWidgetItem(text.isEmpty() ? "" : text);
    item->setTextAlignment(alignment);
    table->setItem(row, col, item);
}

void TableUtils::restTable(QTableWidget* table, int minRow) {
    table->model()->removeRows(0, table->rowCount());
    table->setRowCount(minRow);
}
